package com.leyou.auth.web;

import com.leyou.auth.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserAuthController {

    @Autowired
    private UserAuthService userService;


    /**
     * 用户登录授权
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Void> userLoginForToken(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletResponse response) {

        this.userService.userLoginAuthorization(username, password,response);
        return ResponseEntity.status(204).build();
    }


    /**
     * 用户退出登录
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity<Void> userLoginOut(
            HttpServletRequest request,
            HttpServletResponse response) {

        this.userService.userLoginOut(request, response);
        return ResponseEntity.status(204).build();
    }
}
